/**
 *  @file  AsynSocketHandle.h version 1.0 2007-11-20
 *  @brief Define wrapper class AsynSocketHandle for Nonblocking socket.
 *  Copyright (c) 2007 by @author Zhao Jun <abitfree@gmail.com>
 */

#ifndef __ASYN_SOCKET_HANDLE_H__
#define __ASYN_SOCKET_HANDLE_H__

#include "SocketHandle.h"


namespace vs {
namespace net {

/**
@brief Base class for non-blocking TCP/UDP stream.
This class encapsulates the underlying socket.
*/
class AsynSocketHandle : public SocketHandle
{
public:
	/// Explicit constructor.
	explicit AsynSocketHandle(SOCKET_HANDLE handle = INVALID_SOCKET_HANDLE);

	/// Create a socket.
	bool Create(int domain = AF_INET, int type = SOCK_STREAM, int protocol = 0);
};

INLINE
AsynSocketHandle::AsynSocketHandle(SOCKET_HANDLE handle)
{
	if (Attach(handle))
	{
		ToNonblocking(GetHandle());
	}
}

INLINE
bool AsynSocketHandle::Create(int domain, int type, int protocol)
{
	if (SocketHandle::Create(domain, type, protocol))
	{
		ToNonblocking(GetHandle());
	}

	return IsValid();
}

}	// namespace net
}	// namespace vs

#endif	// __ASYN_SOCKET_HANDLE_H__
